package orest.action.back.userneeds;

import java.sql.Connection;
import java.sql.SQLException;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.util.DateUtility;
import net.ssta.core.web.RequestKit;
import orest.back.userneeds.UserNeedsSql;
 
public class UserNeedsHandler extends HandlerBase
{
    
    /**
     * 获取用户项目需求列表
     */
    public RecordSet userKjcjNeedsList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getKjcgNeedsList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取用户项目需求列表
     */
    public void handKjcgNeeds(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String kjcgUuids = requestKit.getParameter("kjcgUuids", "");
        	String content = requestKit.getParameter("D_CONTENT", "");
        	
        	String uuidsArr[] = kjcgUuids.split(",");
        	for(int i=0;i<uuidsArr.length;i++){
        		String uuid = uuidsArr[i];
        		if(uuid!=null&&!uuid.equals("")){
        			Record record = new Record();
        			record.setEntityName("T_KjcgNeedProcess");
        			record.setConnection(conn);
        			record.setPrimaryKey("ID");
        			record.addData("NEEDID", uuid);
        			record.addData("CONTENT",content);
        			record.addData("SUBMITDATE",DateUtility.getCurrentDateTime());
        			// TODO record.add 当前登陆人的uuid
        			record.create();
        		}
        	}
        	
        	//将用户项目需求字段标记为已处理
        	if(uuidsArr!=null&&uuidsArr.length>0){
	        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
	        	String sql = UserNeedsSql.updateKjcgStatus("1",kjcgUuids);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    /**
     * 标记未处理用户项目需求
     */
    public void unHandKjcgNeeds(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String kjcgUuids = requestKit.getParameter("kjcgUuids", "");
        	
        	//将用户项目需求字段标记为未处理
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String kjcguuidArr[] = kjcgUuids.split(",");
        	if(kjcguuidArr!=null&&kjcguuidArr.length>0){
	        	String sql = UserNeedsSql.updateKjcgStatus("0",kjcgUuids);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 获取用户专家需求列表
     */
    public RecordSet userExpertNeedsList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getExpertNeedsList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 处理用户专家需求
     */
    public void handExpertNeeds(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String expertUuids = requestKit.getParameter("expertNeedUuids", "");
        	String content = requestKit.getParameter("D_CONTENT", "");
        	
        	String uuidsArr[] = expertUuids.split(",");
        	for(int i=0;i<uuidsArr.length;i++){
        		String uuid = uuidsArr[i];
        		if(uuid!=null&&!uuid.equals("")){
        			Record record = new Record();
        			record.setEntityName("T_EXPERTNEEDPROCESS");
        			record.setConnection(conn);
        			record.setPrimaryKey("ID");
        			record.addData("NEEDID", uuid);
        			record.addData("CONTENT",content);
        			record.addData("SUBMITDATE",DateUtility.getCurrentDateTime());
        			record.addData("USERID",LoginUser.getUserId());
        			// TODO record.add 当前登陆人的uuid
        			record.create();
        		}
        	}
        	
        	//将用户专家需求字段标记为已处理
        	if(uuidsArr!=null&&uuidsArr.length>0){
	        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
	        	String sql = UserNeedsSql.updateExpertStatus("1",expertUuids);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 标记未处理用户专家需求
     */
    public void unHandExpertNeeds(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String expertNeedUuids = requestKit.getParameter("expertNeedUuids", "");
        	
        	//将用户专家需求字段标记为未处理
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String expertNeedUuidArr[] = expertNeedUuids.split(",");
        	if(expertNeedUuidArr!=null&&expertNeedUuidArr.length>0){
	        	String sql = UserNeedsSql.updateExpertStatus("0",expertNeedUuids);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    
    /**
     * 从T_FieldOption获取字段选项
     */
    public RecordSet getFildOption(String tablename,String fieldname)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getFildOption(tablename,fieldname);
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    /**
     * 从zdxx表获取字段选项
     */
    public RecordSet getZdxx(String code)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getZdxx(code);
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 从province表获取所有省
     */
    public RecordSet getAllProvince()
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = UserNeedsSql.getAllProvince();
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
}
